package com.cuanbo.dualscreenteachzhu.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.cuanbo.dualscreenteachzhu.utils.UsbTouchHepler;

public class TouchView extends View implements View.OnTouchListener {
    private UsbTouchHepler usbTouchHepler;
    private GestureDetector gestureDetector;

    public TouchView(Context context) {
        this(context,null);
    }

    public TouchView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TouchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public TouchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        gestureDetector = new GestureDetector(getContext(),new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onDown(MotionEvent e) {
                Log.i("luohao","onDown");
                usbTouchHepler.moveMouse((int)e.getX(),(int)e.getY(),true,false);
                return true;
            }

            @Override
            public void onShowPress(MotionEvent e) {
                Log.i("luohao","onShowPress");
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {//点击
                Log.i("luohao","点击");
                usbTouchHepler.moveMouse((int)e.getX(),(int)e.getY(),true,false);
                usbTouchHepler.moveMouse((int)e.getX(),(int)e.getY(),false,false);
                return true;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                Log.i("luohao","移动distanceY="+distanceY);
                if(e2.getPointerCount()==1){
                    usbTouchHepler.moveMouse((int)e2.getX(),(int)e2.getY(),true,false);
                }else{
                    if(distanceY>0){
                        usbTouchHepler.mouseWheel(127);
                    }else if(distanceY<0){
                        usbTouchHepler.mouseWheel(-1);
                    }
                }
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                Log.i("luohao","长按");
                usbTouchHepler.moveMouse((int)e.getX(),(int)e.getY(),false,true);
                usbTouchHepler.moveMouse((int)e.getX(),(int)e.getY(),false,false);
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                Log.i("luohao","onFling");
                usbTouchHepler.moveMouse((int)e2.getX(),(int)e2.getY(),false,false);
                return true;
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Log.i("luohao","双击");
                usbTouchHepler.moveMouse((int)e.getX(),(int)e.getY(),true,false);
                usbTouchHepler.moveMouse((int)e.getX(),(int)e.getY(),false,false);
                return true;
            }
        });
        setOnTouchListener(this);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                usbTouchHepler=new UsbTouchHepler(getContext());
            }
        },2000);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }



}
